package saei.utils;

import java.util.Map;

import javax.servlet.http.HttpSession;

import saei.bean.Usuario;

public class SessionUtils {
	@SuppressWarnings("unchecked")
	public static String getParamValue(HttpSession session, String paramName) {
		Map<String, String> values = (Map<String, String>)session.getAttribute("values");
		return values != null && values.get(paramName) != null ? values.get(paramName) : "";
	}
	
	public static String getAtribValue(HttpSession session, String attName) {
		Object retVal = session.getAttribute(attName);
		return retVal != null ? retVal.toString() : "";
	}
	
	public static String isVisible(HttpSession session, String paramName) {
		boolean isVisible = (boolean) session.getAttribute(paramName);
		return isVisible ? "style='display:block'" : "style='display:none'";
	}
	
	public static void setLogedUser(HttpSession session, Usuario usuario) {
		session.setAttribute("logedUser", usuario);
	}
	
	public static Usuario getLogedUser(HttpSession session) {
		return (Usuario) session.getAttribute("logedUser");
	}
	
	public static void cleanLogedUser(HttpSession session) {
		session.removeAttribute("logedUser");
	}
	
	public static String MD5(String md5) {
	   try {
	        java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
	        byte[] array = md.digest(md5.getBytes());
	        StringBuffer sb = new StringBuffer();
	        for (int i = 0; i < array.length; ++i) {
	          sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
	       }
	        return sb.toString();
	    } catch (java.security.NoSuchAlgorithmException e) {
	    }
	    return null;
	}
}
